package com.example.lt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lt.constant.MessageConstant;
import com.example.lt.exception.CollectException;
import com.example.lt.mapper.CollectMapper;
import com.example.lt.pojo.dto.CollectDTO;
import com.example.lt.pojo.entity.Collect;
import com.example.lt.pojo.entity.Song;
import com.example.lt.pojo.vo.SongVO;
import com.example.lt.result.Result;
import com.example.lt.service.CollectService;
import com.example.lt.service.SongService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/08/16:43
 * version 1.0 注释：
 **/
@Service
@RequiredArgsConstructor
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    private final CollectMapper collectMapper;

    private final SongService songService;

    /**
     * 获取指定用户id的收藏列表歌曲信息
     * @param userId 用户id
     * @return
     */
    public List<SongVO> getCollectSong(Integer userId) {
        // 判断用户是否有收藏的歌曲
        List<Collect> collects = lambdaQuery().eq(Collect::getUserId, userId).list();
        if (collects.size() == 0) {
            return null;
        }

        // 通过stream流的方式把 songId 属性抽离成集合，然后查询song的信息
        List<Integer> songIds = collects.stream().map(Collect::getSongId).collect(Collectors.toList());
        List<Song> songs = songService.listByIds(songIds);

        List<SongVO> songVOList = new ArrayList<>();
        for (Song song : songs) { // 把Song中相应的属性复制到SongVO中
            SongVO songVO = BeanUtil.copyProperties(song,SongVO.class);
            songVOList.add(songVO);
        }

        return songVOList;
    }

    /**
     * 添加收藏歌曲记录
     * @param collectDTO
     * @return
     */
    public Result addCollect(CollectDTO collectDTO) {
        if (collectDTO.getSongId() == null) {
            throw new CollectException(MessageConstant.COLLECT_FAIL);
        }
        Collect collect = BeanUtil.copyProperties(collectDTO, Collect.class);
        collectMapper.insert(collect);
        return Result.success(MessageConstant.COLLECT_SUCCESS, true);
    }

    /**
     * 删除收藏歌曲记录
     * @param userId
     * @param songId
     * @return
     */
    public Result deleteCollect(Integer userId, Integer songId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("song_id", songId);
        int flag = collectMapper.delete(queryWrapper);
        if (flag > 0) {
            return Result.success(MessageConstant.CANCEL_COLLECT_SUCCESS, false);
        } else {
            throw new CollectException(MessageConstant.CANCEL_COLLECT_FAIL);
        }
    }

    /**
     * 判断当前用户有没有收藏这首歌曲
     * @param collectDTO
     * @return
     */
    public Result status(CollectDTO collectDTO) {
        List<Collect> collects = lambdaQuery()
                .eq(Collect::getUserId, collectDTO.getUserId())
                .eq(Collect::getSongId, collectDTO.getSongId())
                .list();
        if (collects.size() > 0) {
            return Result.success(MessageConstant.IS_COLLECT,true);
        } else {
            return Result.success(MessageConstant.NOT_COLLECT,false);
        }
    }

}
